package com.cas.scalls.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil {
	private static String soap_model_withparma = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:#method# soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://loushang.ws\"><ns1:arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">#arg0#</ns1:arg0></ns1:#method#></soapenv:Body></soapenv:Envelope>";
	private static String soap_model_withoutparma = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:#method# soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://loushang.ws\"></ns1:#method#></soapenv:Body></soapenv:Envelope>";

	public static String doPost(String urlStr, String xmlInfo, String type) {
		StringBuffer br = new StringBuffer();
		try {
			String urlstring = type;
			if (urlstring.indexOf("connectable") >= 0) {
				URL url = new URL(urlStr);
				URLConnection con = url.openConnection();
				con.setDoOutput(true);
				con.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				con.setRequestProperty("Accept",
						"application/soap+xml, application/dime, multipart/related, text/*");
				con.setRequestProperty("User-Agent", "Axis/1.4");
				con.setRequestProperty("SOAPAction", "\"\"");
				String send = HttpUtil.soap_model_withoutparma;
				send.replace("#method#", "connectable");
				OutputStreamWriter out = new OutputStreamWriter(
						con.getOutputStream());
				out.write(new String(send.getBytes("utf-8")));
			} else if (urlstring.indexOf("getSystemTime") >= 0) {
				URL url = new URL(urlStr);
				URLConnection con = url.openConnection();
				con.setDoOutput(true);
				con.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				con.setRequestProperty("Accept",
						"application/soap+xml, application/dime, multipart/related, text/*");
				con.setRequestProperty("User-Agent", "Axis/1.4");
				con.setRequestProperty("SOAPAction", "\"\"");
				String send = HttpUtil.soap_model_withoutparma;
				send.replace("#method#", "getSystemTime");
				OutputStreamWriter out = new OutputStreamWriter(
						con.getOutputStream());
				out.write(new String(send.getBytes("utf-8")));
			} else if (urlstring.indexOf("downloadInInfo") >= 0) {
				URL url = new URL(urlStr);
				URLConnection con = url.openConnection();
				con.setDoOutput(true);
				con.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				con.setRequestProperty("Accept",
						"application/soap+xml, application/dime, multipart/related, text/*");
				con.setRequestProperty("User-Agent", "Axis/1.4");
				con.setRequestProperty("SOAPAction", "\"\"");
				String send = HttpUtil.soap_model_withparma;
				send.replace("#method#", "downloadInInfo");
				send.replace("#arg0#", xmlInfo);
				OutputStreamWriter out = new OutputStreamWriter(
						con.getOutputStream());
				out.write(new String(send.getBytes("utf-8")));
				out.flush();
				out.close();
				br.append(new BufferedReader(new InputStreamReader(con
						.getInputStream())));

			} else if (urlstring.indexOf("uploadTranInfo") >= 0) {
				URL url = new URL(urlStr);
				URLConnection con = url.openConnection();
				con.setDoOutput(true);
				con.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				con.setRequestProperty("Accept",
						"application/soap+xml, application/dime, multipart/related, text/*");
				con.setRequestProperty("User-Agent", "Axis/1.4");
				con.setRequestProperty("SOAPAction", "\"\"");
				String send = HttpUtil.soap_model_withparma;
				send.replace("#method#", "uploadTranInfo");
				send.replace("#arg0#", xmlInfo);
				OutputStreamWriter out = new OutputStreamWriter(
						con.getOutputStream());
				out.write(new String(send.getBytes("utf-8")));
				out.flush();
				out.close();
				br.append(new BufferedReader(new InputStreamReader(con
						.getInputStream())));

			} else if (urlstring.indexOf("uploadInInfo") >= 0) {
				URL url = new URL(urlStr);
				URLConnection con = url.openConnection();
				con.setDoOutput(true);
				con.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				con.setRequestProperty("Accept",
						"application/soap+xml, application/dime, multipart/related, text/*");
				con.setRequestProperty("User-Agent", "Axis/1.4");
				con.setRequestProperty("SOAPAction", "\"\"");
				String send = HttpUtil.soap_model_withparma;
				send.replace("#method#", "uploadInInfo");
				send.replace("#arg0#", xmlInfo);
				OutputStreamWriter out = new OutputStreamWriter(
						con.getOutputStream());
				out.write(new String(send.getBytes("utf-8")));
				out.flush();
				out.close();
				br.append(new BufferedReader(new InputStreamReader(con
						.getInputStream())));

			} else {
				// qDebug()<<url.path()+"  sorry url don't defined!";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return br.toString();
	}
}
